import { arrayCompare, wordCountAndSort } from '@/lib/utils';
import { getArticles, getClsArticles } from '@/server/controllers/articleController';
import { tokenize } from '@/server/controllers/jiebaController';
import { getSecuBlocks, getSecuInfo, getTopRank, getKplRecentBlocks } from '@/server/controllers/secuController';
import { fetchTopRank } from '@/lib/kpl';

const boxItemStyle = {
    width: '30%'
};

const overFlowStyle = {
    overflow: 'hidden',
    textOverflow: 'ellipsis',
    whiteSpace: 'nowrap',
    width: '100%'
};

const InfoCrossNews = async ({ blocks }) => {

    const vibBlocks = [];
    const topRanks = await fetchTopRank(5);
    for (let t of topRanks) {
        for (let b of blocks) {
            const topSting = t.toLowerCase();
            const blockNameSting = b.block_name.toLowerCase();
            if (topSting.includes(blockNameSting) || blockNameSting.includes(topSting)) {
                vibBlocks.push(b);
            }
        }
    }
    const vibBlockNames = vibBlocks.map((b) => b.block_name);
    return (
        <div style={{ borderTop: '1px solid' }}>
            <p style={{ color: 'white' }}>{vibBlockNames.join('；') || '---------'}</p>
            {blocks.map((b, index) => (
                <div style={overFlowStyle} key={index}>
                    <span>
                        {b.block_name} ：{b.block_explain}
                    </span>
                </div>
            ))}
        </div>
    );
};

const InfoPage = async ({ params }) => {
    const { secuCode } = await params;
    const info = await getSecuInfo(secuCode);
    if (!info) return null;
    let tempBlocks = [];
    const allSecuBlocks = await getSecuBlocks(secuCode);
    const kplRecentBlocks = await getKplRecentBlocks();
    for (const block of allSecuBlocks) {
        for (let r of kplRecentBlocks) {
            if (r.toLowerCase().includes(block.block_name.toLowerCase())) {
                tempBlocks.push(block);
            }
            if (block.block_name.toLowerCase().includes(r.toLowerCase())) {
                tempBlocks.push(block);
            }
        }
    }
    const blocks = tempBlocks.filter(
        (value, index, selfArr) => index === selfArr.findIndex((el) => el.block_name === value.block_name)
    );
    const expect_blocks = blocks.filter((b) => info.expect.includes(b.block_name));
    const unique_expect_blocks = expect_blocks.filter(
        (value, index, selfArr) => index === selfArr.findIndex((el) => el.block_name === value.block_name)
    );
    // console.log(info.expect);
    // console.log(expect_blocks);
    const styles = blocks
        .filter(
            (b) =>
                b.block_name.includes('重组') ||
                b.block_name.includes('摘帽') ||
                b.block_name.includes('国企') ||
                b.block_name.includes('央企') ||
                b.block_name.includes('增长')
        )
        .map((b) => b.block_name)
        .join('；');
    const cls_blocks = blocks
        .filter((b) => b.block_source === 'cls')
        .map((b) => b.block_name)
        .join('；');
    const kpl_blocks = blocks
        .filter((b) => b.block_source === 'kpl' && !cls_blocks.includes(b.block_name))
        .map((b) => b.block_name)
        .join('；');
    const ths_blocks = blocks
        .filter((b) => b.block_source === 'ths' && !cls_blocks.includes(b.block_name) && !kpl_blocks.includes(b.block_name))
        .map((b) => b.block_name)
        .join('；');
    return (
        <div
            style={{
                display: 'flex',
                flexDirection: 'column'
            }}
        >
            <div
                style={{
                    display: 'flex',
                    flexDirection: 'column',
                    borderBottom: '1px solid'
                }}
            >
                <div name="row1" style={{ display: 'flex', flexDirection: 'row' }}>
                    <div style={boxItemStyle}>{info.secu_code}</div>
                    <div style={boxItemStyle}>{info.secu_name}</div>
                    <div style={boxItemStyle}>
                        {info.hy} {'=>'} {info.hy2}
                    </div>
                </div>
                <div name="row2" style={{ display: 'flex', flexDirection: 'row' }}>
                    <div style={boxItemStyle}>市场：{info.secu_code.startsWith('6') ? '沪' : '深'}</div>
                    <div style={boxItemStyle}>流通：{info.ltsz}</div>
                    <div style={boxItemStyle}>价格：{parseFloat(info.price.toFixed(2))}</div>
                    <div style={boxItemStyle}></div>
                </div>
            </div>
            <div>
                <span style={{ color: 'white', display: 'block' }}>风格 ：{styles}</span>
                <span style={{ color: 'cyan', display: 'block' }}>财联社 ：{cls_blocks}</span>
                <span style={{ color: 'magenta', display: 'block' }}>INFO : {info.expect?.join('；')}</span>
                <span style={{ color: 'red', display: 'block' }}>KPL ：{kpl_blocks}</span>
                <span style={{ color: 'red', display: 'block' }}>THS ：{ths_blocks}</span>
            </div>
            <div style={overFlowStyle}>
                <span>描述 ：{info.describe}</span>
            </div>
            <div style={overFlowStyle}>
                <span>亮点 ：{info.profitable_business}</span>
            </div>
            <div style={overFlowStyle}>
                <span>主营 ：{info.main_business}</span>
            </div>
            <div style={overFlowStyle}>
                <span>产品 ：{info.product_name}</span>
            </div>
            <div style={{ fontSize: '0.6rem' }}>
                <InfoCrossNews blocks={blocks} />
                {/*<hr />*/}
                <div>
                    {unique_expect_blocks.map((b) => {
                        return (
                            <div style={overFlowStyle} key={b.block_code}>
                                <p style={{ color: 'magenta' }}>
                                    {b.block_name} ：<span style={{ color: 'cyan' }}> {b.block_explain}</span>
                                </p>
                            </div>
                        );
                    })}
                </div>
            </div>
        </div>
    );
};

export default InfoPage;
